package edu.fudan.cse.medlab.event.moresite;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class GetCandidateSite {
//	public static String[] getFromPerfixFile(String prefix,String candidatesite){
//		try{
//			BufferedReader bin = new BufferedReader(new FileReader(prefix));
//			BufferedWriter bout = new BufferedWriter(new FileWriter(candidatesite));
//			String sites="";
//			String s;
//			while( (s= bin.readLine())!=null){
//				s = s.substring(0,s.indexOf("\\"));
//				if(!sites.contains(s))
//					sites += s+"!";
//			}
//			String[] sitename = sites.substring(0,sites.length()-1).split("!");
//			for (String string : sitename) {
//				bout.write(string+"\r\n");
//				bout.flush();
//			}
//			bin.close();
//			bout.close();
//			return sitename;
//		}
//		catch(Exception e){
//			e.printStackTrace();
//			return null;
//		}
//	}
	
	public static String[] getFromSiteFile(String candidatesite,String blacklist){
		try{
			String blocksite="";
			String s;
			BufferedReader bin = new BufferedReader(new FileReader(blacklist));
			while( (s= bin.readLine())!=null){
				s = s.trim();
				blocksite+= s+"!";
			}
			bin.close();
			
			bin = new BufferedReader(new FileReader(candidatesite));
			String sites="";
			while( (s= bin.readLine())!=null){
				if(s.contains(":"))
					s = s.substring(0,s.indexOf(":"));
				if(s.contains(" "))
					s = s.substring(0,s.indexOf(" "));
				if(s.contains("\t"))
					s = s.substring(0,s.indexOf("\t"));
				s = s.trim();
				if(!sites.contains(s)&&!blocksite.contains(s))
					sites += s+"!";
			}
			String[] sitename = sites.substring(0,sites.length()-1).split("!");
			bin.close();
			return sitename;
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public static void getDistinctSiteFromFile(String prefixfile,String sitefile){
		try{
		
			String s;
			BufferedReader bin = new BufferedReader(new FileReader(prefixfile));
			
			String sites="";
			while( (s= bin.readLine())!=null){
				if(s.contains(" "))
					s = s.substring(0,s.indexOf(" "));
				if(s.contains("\t"))
					s = s.substring(0,s.indexOf("\t"));
				s = s.trim();
				if(s.contains("\\"))
						s=s.substring(0,s.indexOf("\\"));
				if(!sites.contains(s))
					sites += s+"!";
			}
			String[] sitename = sites.substring(0,sites.length()-1).split("!");
			bin.close();
			BufferedWriter bout = new BufferedWriter(new FileWriter(sitefile));
			for (String string : sitename) {
				bout.write(string+"\r\n");
			}
			bout.close();
			return;
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	public static void main(String[] args){
//		String[] sites = GetCandidateSite.getFromSiteFile("candidatesite.txt","candidatesite_greedy.txt");
//		for (String string : sites) {
//			System.out.println(string);
//		}
		String prefixfile="prefix(title+loc).txt";
		String sitefile="site(title+loc).txt";
		GetCandidateSite.getDistinctSiteFromFile(prefixfile, sitefile);
	}
}
